package com.qlzb.taskmanagement.service.cron.impl;

import com.qlzb.taskmanagement.entity.CronInfoEntity;
import com.qlzb.taskmanagement.pojo.OutputWrapper;
import com.qlzb.taskmanagement.repositoty.CronInfoRepository;
import com.qlzb.taskmanagement.service.cron.CronInfoService;
import com.qlzb.taskmanagement.utils.SnowflakeUtils;
import com.qlzb.taskmanagement.vo.form.CronInfoFormVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Transactional
@Service
public class CronInfoServiceImpl implements CronInfoService {

    @Autowired
    private CronInfoRepository cronInfoRepository;

    @Override
    public OutputWrapper findById(String id) {
        CronInfoEntity cronInfoEntity = cronInfoRepository.findByByPrimaryKey(id);
        if (!StringUtils.isEmpty(cronInfoEntity)) {
            return OutputWrapper.markSelectSuccess(cronInfoEntity);
        }

        return OutputWrapper.markSelectError();
    }

    @Override
    public OutputWrapper add(CronInfoFormVo vo) {
        CronInfoFormVo cronInfoFormVo = CronInfoFormVo.class.cast(vo);
        CronInfoEntity cronInfoEntity = new CronInfoEntity();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        long id = SnowflakeUtils.nextId();

        cronInfoEntity.setId(String.valueOf(id));
        cronInfoEntity.setCron(cronInfoFormVo.getCron());
        cronInfoEntity.setQuarzName(cronInfoFormVo.getQuarzName());
        cronInfoEntity.setUrl(cronInfoFormVo.getUrl());
        cronInfoEntity.setCreateTime(time);

        cronInfoRepository.save(cronInfoEntity);

        return OutputWrapper.markInsertSuccess(1);
    }

    @Override
    public OutputWrapper modify(CronInfoFormVo vo) {
        CronInfoFormVo cronInfoFormVo = CronInfoFormVo.class.cast(vo);
        CronInfoEntity cronInfoEntity = cronInfoRepository.findByByPrimaryKey(vo.getId());

        cronInfoEntity.setCron(cronInfoFormVo.getCron());
        cronInfoEntity.setQuarzName(cronInfoFormVo.getQuarzName());
        cronInfoEntity.setUrl(cronInfoFormVo.getUrl());

        cronInfoRepository.save(cronInfoEntity);

        return OutputWrapper.markInsertSuccess(1);
    }

    @Override
    public OutputWrapper removeById(String id) {
        CronInfoEntity cronInfoEntity = cronInfoRepository.findByByPrimaryKey(id);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        byte delFlag = 1;

        cronInfoEntity.setId(id);
        cronInfoEntity.setDelFlag(delFlag);
        cronInfoEntity.setUpdateTime(time);

        cronInfoRepository.save(cronInfoEntity);
        return OutputWrapper.markInsertSuccess(1);
    }

    @Override
    public List<CronInfoEntity> findAll(){
        List<CronInfoEntity> cronInfoEntityList = cronInfoRepository.findAll();
        return cronInfoEntityList;
    }
}
